<script setup name="BizBillInfoDetailsIndex">
import eventBus from "@/utils/eventBus";
import { usePagination } from "@/utils/composables";
import FormMode from "./modules/FormMode/index.vue";
import { parseTime } from "@/utils/aidex";
import { listBizBillInfoDetails, delBizBillInfoDetails, exportBizBillInfoDetails } from '@/api/biz/bizBillInfoDetails'
const { pagination, getParams, onSizeChange, onCurrentChange, onSortChange } =
  usePagination();
const router = useRouter();

const data = ref({
  loading: false,
  /**
   * 详情展示模式
   * router 路由跳转
   * dialog 对话框
   * drawer 抽屉
   */
  formMode: "drawer",
  // 详情
  formModeProps: {
    visible: false,
    id: ""
  },
  // 搜索
  search: {
    userId: "",
    deptId: "",
    userName: "",
    deptName: "",
  },
  // 批量操作
  batch: {
    enable: false,
    selectionDataList: []
  },
  // 列表数据
  dataList: []
});

// 页面挂载时 调用
onMounted(() => {
  getDataList();
  if (data.value.formMode === "router") {
    eventBus.on("get-data-list", () => {
      getDataList();
    });
  }
});
//
onBeforeUnmount(() => {
  if (data.value.formMode === "router") {
    eventBus.off("get-data-list");
  }
});

// 每页数量切换
function sizeChange(size) {
  onSizeChange(size).then(() => getDataList());
}

// 当前页码切换（翻页）
function currentChange(page = 1) {
  onCurrentChange(page).then(() => getDataList());
}

// 字段排序
function sortChange(prop, order) {
  onSortChange(prop, order).then(() => getDataList());
}
// 查询
function getDataList() {
  data.value.loading = true;
  let params = getParams();
  data.value.search.postName && (params.name = data.value.search.postName);
  listBizBillInfoDetails(params).then((res) => {
    data.value.loading = false;
    data.value.dataList = res.data.list;
    pagination.value.total = res.data.total;
  });
}

// 编辑
function onEdit(row) {
  data.value.formMode = "drawer";
  if (data.value.formMode === "router") {
    router.push({
      name: "routerName",
      params: {
        id: row.id
      }
    });
  } else {
    data.value.formModeProps.id = row.id;
    data.value.formModeProps.visible = true;
  }
}
// 添加
function onAdd() {
  data.value.formModeProps.id = "";
  data.value.formMode = "dialog";
  data.value.formModeProps.visible = true;
}
// 批量删除 单独删除
function onDel(row) {
  const ids =
    row.id ||
    data.value.batch.selectionDataList.map((item) => {
      return item.id;
    });
  const names =
    row.postName ||
    data.value.batch.selectionDataList.map((item) => {
      return item.postName;
    });
   if (ids.length > 0) {
  ElMessageBox.confirm("确认删除「" + names + "」吗？", "确认信息")
    .then(() => {
      delPost(ids).then((res) => {
        getDataList();
        if (res.code == 200)
          ElMessage.success({
            message: "删除成功",
            center: true
          });
      });
    })
    .catch(() => {});
    } else {
        ElMessage.warning({ message: "请选择数据", center: true });
      }
}
</script>

<template>
  <div>
    <page-main style="height: calc(100vh - 160px); margin: 10px">
      <search-bar>
        <el-form
          :model="data.search"
          size="default"
          label-width="auto"
          label-suffix=""
        >
          <el-row :gutter="30">

          <el-col :span="5">
                        <el-form-item label="创建者">
                          <el-input
                            v-model="data.search.userId"
                            placeholder="请输入创建者"
                            clearable
                            @keydown.enter="currentChange()"
                            @clear="currentChange()"
                          />
                        </el-form-item>
                      </el-col>

          <el-col :span="5">
                        <el-form-item label="创建部门">
                          <el-input
                            v-model="data.search.deptId"
                            placeholder="请输入创建部门"
                            clearable
                            @keydown.enter="currentChange()"
                            @clear="currentChange()"
                          />
                        </el-form-item>
                      </el-col>

          <el-col :span="5">
                        <el-form-item label="创建名称">
                          <el-input
                            v-model="data.search.userName"
                            placeholder="请输入创建名称"
                            clearable
                            @keydown.enter="currentChange()"
                            @clear="currentChange()"
                          />
                        </el-form-item>
                      </el-col>

          <el-col :span="5">
                        <el-form-item label="部门名称">
                          <el-input
                            v-model="data.search.deptName"
                            placeholder="请输入部门名称"
                            clearable
                            @keydown.enter="currentChange()"
                            @clear="currentChange()"
                          />
                        </el-form-item>
                      </el-col>


            <el-col :span="5">
              <el-form-item>
                <el-button type="primary" @click="currentChange()">
                  <template #icon>
                    <el-icon>
                      <svg-icon name="ep:search" />
                    </el-icon>
                  </template>
                  查询
                </el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </search-bar>
      <el-row>
        <el-col :span="24" style="text-align: right">
          <el-button-group>
            <el-button type="primary" @click="onAdd">
              <template #icon>
                <el-icon>
                  <svg-icon name="ep:circle-plus" />
                </el-icon>
              </template>
              新增
            </el-button>
            <el-button type="danger" @click="onDel">
              <template #icon>
                <el-icon>
                  <svg-icon name="ep:close-bold" :size="20" />
                </el-icon>
              </template>
              删除
            </el-button>
          </el-button-group>
        </el-col>
      </el-row>
      <div style="height: calc(100vh - 380px); overflow-y: auto">
        <el-table
          ref="table"
          v-loading="data.loading"
          class="list-table"
          :data="data.dataList"
          border
          highlight-current-row
          row-key="id"
          @sort-change="sortChange"
          @selection-change="data.batch.selectionDataList = $event"
        >
          >
          <el-table-column type="selection" align="center" />
          <el-table-column
            type="index"
            align="center"
            label="序号"
            width="60"
          />
          <el-table-column prop="userId" label="创建者"
          show-overflow-tooltip width="120" align="center">
          <template v-slot="scope">
          {{scope.row.userId}}
          </template>
          </el-table-column>
          <el-table-column prop="deptId" label="创建部门"
          show-overflow-tooltip width="120" align="center">
          <template v-slot="scope">
          {{scope.row.deptId}}
          </template>
          </el-table-column>
          <el-table-column prop="userName" label="创建名称"
          show-overflow-tooltip width="120" align="center">
          <template v-slot="scope">
          {{scope.row.userName}}
          </template>
          </el-table-column>
          <el-table-column prop="deptName" label="部门名称"
          show-overflow-tooltip width="120" align="center">
          <template v-slot="scope">
          {{scope.row.deptName}}
          </template>
          </el-table-column>
          <el-table-column prop="companyId" label="公司ID"
          show-overflow-tooltip width="120" align="center">
          <template v-slot="scope">
          {{scope.row.companyId}}
          </template>
          </el-table-column>
          <el-table-column prop="ordreId" label="订单ID"
          show-overflow-tooltip width="120" align="center">
          <template v-slot="scope">
          {{scope.row.ordreId}}
          </template>
          </el-table-column>
          <el-table-column prop="billId" label="账单ID"
          show-overflow-tooltip width="120" align="center">
          <template v-slot="scope">
          {{scope.row.billId}}
          </template>
          </el-table-column>
          <el-table-column prop="money" label="账单金额"
          show-overflow-tooltip width="120" align="center">
          <template v-slot="scope">
          {{scope.row.money}}
          </template>
          </el-table-column>
          <el-table-column prop="sort" label="排序"
          show-overflow-tooltip width="120" align="center">
          <template v-slot="scope">
          {{scope.row.sort}}
          </template>
          </el-table-column>
          <el-table-column prop="status" label="状态"
          show-overflow-tooltip width="120" align="center">
          <template v-slot="scope">
          {{scope.row.status}}
          </template>
          </el-table-column>
          <el-table-column prop="remark" label="备注"
          show-overflow-tooltip width="120" align="center">
          <template v-slot="scope">
          {{scope.row.remark}}
          </template>
          </el-table-column>
          <el-table-column prop="id" label="主键ID"
          show-overflow-tooltip width="120" align="center">
          <template v-slot="scope">
          {{scope.row.id}}
          </template>
          </el-table-column>
          <el-table-column prop="createBy" label="创建者"
          show-overflow-tooltip width="120" align="center">
          <template v-slot="scope">
          {{scope.row.createBy}}
          </template>
          </el-table-column>
          <el-table-column prop="createDept" label="创建部门"
          show-overflow-tooltip width="120" align="center">
          <template v-slot="scope">
          {{scope.row.createDept}}
          </template>
          </el-table-column>
          <el-table-column prop="createTime" label="创建时间"
          show-overflow-tooltip width="120" align="center">
          <template v-slot="scope">
          {{scope.row.createTime}}
          </template>
          </el-table-column>
          <el-table-column prop="updateBy" label="更新者"
          show-overflow-tooltip width="120" align="center">
          <template v-slot="scope">
          {{scope.row.updateBy}}
          </template>
          </el-table-column>
          <el-table-column prop="updateTime" label="更新时间"
          show-overflow-tooltip width="120" align="center">
          <template v-slot="scope">
          {{scope.row.updateTime}}
          </template>
          </el-table-column>
          <el-table-column prop="updateIp" label="更新IP"
          show-overflow-tooltip width="120" align="center">
          <template v-slot="scope">
          {{scope.row.updateIp}}
          </template>
          </el-table-column>
          <el-table-column prop="version" label="版本"
          show-overflow-tooltip width="120" align="center">
          <template v-slot="scope">
          {{scope.row.version}}
          </template>
          </el-table-column>
          <el-table-column prop="delFlag" label="删除标志"
          show-overflow-tooltip width="120" align="center">
          <template v-slot="scope">
          {{scope.row.delFlag}}
          </template>
          </el-table-column>
          <el-table-column label="操作" width="160" align="center">
            <template #default="scope">
              <el-button-group>
                <el-button
                  type="primary"
                  size="small"
                  text
                  @click="onEdit(scope.row)"
                >
                  <template #icon>
                    <el-icon>
                      <svg-icon name="ant-design:edit-filled" />
                    </el-icon>
                  </template>
                  编 辑</el-button
                >

                <el-button
                  type="danger"
                  text
                  size="small"
                  @click="onDel(scope.row)"
                >
                  <template #icon>
                    <el-icon>
                      <svg-icon name="ep:close-bold" />
                    </el-icon>
                  </template>
                  删除
                </el-button>
              </el-button-group>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <el-pagination
        :current-page="pagination.page"
        :total="pagination.total"
        :page-size="pagination.size"
        :page-sizes="pagination.sizes"
        :layout="pagination.layout"
        :hide-on-single-page="false"
        class="pagination"
        background
        @size-change="sizeChange"
        @current-change="currentChange"
      />
    </page-main>
    <FormMode
      v-if="['dialog', 'drawer'].includes(data.formMode)"
      :id="data.formModeProps.id"
      v-model="data.formModeProps.visible"
      :mode="data.formMode"
      @success="getDataList"
    />
  </div>
</template>

<style lang="scss" scoped>
.el-pagination {
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
